package ramakrishnanmoreHealthView;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import ramakrishnanmoreHealthModel.Campus.CampusLocation;

//Draws the Location and the Time where the student has been to on the Map.
public class StudentJourneyGrid extends JFrame {
	// ATTRIBUTES ======================================


	// Set to true if the grid is successfully created and has proper values.
	boolean gridDisplaySuccessx_ = true;

	// The location Names on the Grid.
	private String data[][] = {
			{ "Halls", "Halls", "Halls", "ClassRoom", "Halls" },
			{ "Sidewalk", "Library", "Residence1", "ClassRoom", "Sidewalk" },
			{ "Halls", "Halls", "Residence1", "ClassRoom", "Sidewalk" },
			{ "ClassRoom", "ClassRoom", "Hall", "Sidewalk", "Sidewalk" },
			{ "ClassRoom", "Sidewalk", "Restaurant", "Sidewalk", "Restaurant" } };
	private String col[] = { "1", "2", "3", "4", "5" };

	
	/**
	 * 	 CONSTRUCTORS ======================================
	 * 	 INTENT: Draw the location and the time instance of the places the Student visited during his journey.
	 * 	 PRE-CONDITION :
	 * 	 1. cList has All the places the Student has been to.
	 * 	 2. The location coordinates should be next to each other.
	 * 	 3. The class Names should be similar to corresponding location name on
	 * 	 the Map i.e.if the Location name is XYZ
	 * 	 then Class Name should be XYZ also.
	 * 	 POST-CONDITION:
	 * 	 1. Displays the Location and the Time the student was present at that
	 * 	 location.
	 * 	 2. If the locationType entered by the Student and the location type present on the map do not match then
	 * 	 display a custom message.
	 * @param clList
	 * @param jframeText
	 */
	public StudentJourneyGrid(ArrayList<CampusLocation> clList, String jframeText) // clList ===>
	// campusLocationList
	{

		JFrame frame = new JFrame(jframeText);
		JPanel panel = new JPanel();
		panel.setSize(800,800);
		DefaultTableModel model = new DefaultTableModel(data, col) {
		
		
			// The whole grids cannot be edited by clicking on it.
			public boolean isCellEditable(int row, int column) {
				// TODO Auto-generated method stub
				return false;
			}
		};

		JTable table = new JTable(model);
		// Adjusts the Width and the height of the cells.
		for (int colLength = 0; colLength <= col.length - 1; colLength++) {
			TableColumn column = table.getColumnModel().getColumn(colLength);
			column.setPreferredWidth(180);
		}
		table.setRowHeight(70);

		// Get each Location Information the Student has been to.
		Enumeration<CampusLocation> en = Collections.enumeration(clList);

		while (en.hasMoreElements()) {
			// The Location Information where the Student was.
			CampusLocation cl = (CampusLocation) (en.nextElement());

			// The location name as per the Defined Grid.
			String gridType = data[cl.getLocationLatitude()][cl
					.getLocationlongitude()];

			// the location Name as per what the User entered.
			String locationTypeName = cl.getClass().getSimpleName();

			// If the Student is lying about the Location Name then there will
			// be a mismatch between his location and the pre-defined
			// grid location.
			// The AND operator makes sure that even if there is one mismatch
			// then the Grid has wrong data in it. i.e only T AND T = T.
			if (gridType.equals(locationTypeName)) {
				// String test = gridType + " " + cl.getLocationTime();
				// model.setValueAt(test, cl.getLocationLatitude(),
				// cl.getLocationlongitude());

				// sets the time when the student was present at the location.
				model.setValueAt(gridType + " Visiting Time: "
						+ cl.getLocationTime(), cl.getLocationLatitude(), cl
						.getLocationlongitude());
				
				gridDisplaySuccessx_ = true && gridDisplaySuccessx_;
			} else {
				// sets the time when the student was present at the
				// location.
				model.setValueAt(gridType + " Visiting Time: "
						+ cl.getLocationTime(), cl.getLocationLatitude(), cl
						.getLocationlongitude());
				gridDisplaySuccessx_ = true && gridDisplaySuccessx_;

				// The user is lying about his location name
				// Add the time instance with a custom message in the Location
				// cell.
				// Object obj = model.getValueAt(cl.getLocationLatitude(),
				// cl.getLocationlongitude());
				// model.setValueAt("The location on the Map and Entered by you do not match for "+cl.getLocationTime()
				// + ".", cl.getLocationLatitude(), cl.getLocationlongitude());
				// gridDisplaySuccessx_ = false;
			}

		}

		// Add the Information to the frame.
		panel.add(table);
		frame.add(panel);
		frame.setResizable(true);
		frame.setSize(800, 400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public String[][] getRowDataMapLayout() {
		return this.data;

	}

	public String[] getColumnDataMapLayout() {
		return this.col;
	}

	// If the grid has wrong data then indicate it by returning false.
	// Return whether the grid has correct information about the Student
	// Journey.
	public boolean getgridDisplaySuccess() {
		return gridDisplaySuccessx_;
	}

}
